package com.asa.stream;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class B {

	public static void main(String[] args) {
//		IntStream.range(1, 100).parallel().peek(B::debug).count();//parallel申明并行流
//		
//		IntStream.range(1, 100).parallel().peek(B::debug)//先并行
//		.sequential().peek(B::debug)//再串行
//		.count();//
		
		//流式编程，默认开启的线程数和cpu核数一致
		System.setProperty("java.util.concurrent.ForkJoinPool.commonPool.parallelism","20");//使用这个可以改线程数
		
		//也可以自定义线程池，不共用
		ForkJoinPool pool = new ForkJoinPool(20);
		pool.submit(()->IntStream.range(1, 100).parallel().peek(B::debug).count());
		
		pool.shutdown();
		
		synchronized (pool) {
			try {
				pool.wait();//在这个代码中必须要有，不然ForkJoinPool是守护线程，随着主线程消失而消失了
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		
		
	}
	public static void debug(int i) {
		System.out.println(Thread.currentThread().getName()+":"+i);//可以发现ForkJoinPool.commonPool这个线程池
		try {
			TimeUnit.SECONDS.sleep(3);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
